package com.xup.luckymoney;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;

public class Config {

    public static final String ACTION_RED_PACKET_SERVICE_DISCONNECT = "com.freeme.redpacket.ACCESSIBILITY_DISCONNECT";
    public static final String ACTION_RED_PACKET_SERVICE_CONNECT = "com.freeme.redpacket.ACCESSIBILITY_CONNECT";

    public static final String PREFERENCE_NAME = "config";
    public static final String KEY_ENABLE_WECHAT = "key_wechat_pref";
    public static final String KEY_ENABLE_QQ = "key_qq_pref";
    public static final String KEY_RED_PACKET_DELAY_TIME = "key_red_packet_delay_time";
    public static final String KEY_NOTICE_CATEGORY = "notice_category";
    public static final String KEY_ENABLE_NOTICE = "key_enable_notice";
    public static final String KEY_LISTEN_NOTICE = "key_listen_notice";

    public static final String KEY_ENABLE_FLOAT_WINDOW = "key_float_window_pref";

    public static final String EVNET_WECHAT_AFTER_OPEN_PACKET = "event_after_open_wechat_red_packet";

    public static final String WECHAT_PACKAGENAME = "com.tencent.mm";
    public static final String QQ_PACKAGENAME = "com.tencent.mobileqq";

    public static final String LUCKY_MONEY_ENABLED = "lucky_money_enabled";

    public static final int EVENT_OPEN = 0;
    // look others lucky
    public static final int EVENT_OBSERVE = 1;

    SharedPreferences preferences;
    Editor mEditor;

    private boolean autoClosePacket = false;

    private static Config mInstance = null;
    private Context mContext;

    public int musicIndex;
    public boolean needUpdate = true;
    private MediaPlayer player;

    public int[] nitofyMusicID = {
            R.raw.hongbao_arrived,
            R.raw.redpacket1,
            R.raw.redpacket2,
            R.raw.redpacket3,
            R.raw.redpacket4,
            R.raw.redpacket5,
    };

    private Config() {
    }

    private Config(Context context) {
        mContext = context;
        preferences = mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        mEditor = preferences.edit();
    }

    public static Config getInstance(Context context) {
        synchronized (Config.class) {
            if (mInstance == null) {
                mInstance = new Config(context);
            }
            return mInstance;
        }
    }

    public boolean getNoticeEnable() {
        return preferences.getBoolean(KEY_ENABLE_NOTICE, false);
    }

    public void setNoticeEnable(boolean enable) {
        synchronized (preferences) {
            mEditor.putBoolean(KEY_ENABLE_NOTICE, enable);
            mEditor.apply();
        }
    }

    public boolean getFloatWindowEnable() {
        return preferences.getBoolean(KEY_ENABLE_FLOAT_WINDOW, false);
    }

    public void setFloatWindowEnable(boolean enbale) {
        synchronized (preferences) {
            mEditor.putBoolean(KEY_ENABLE_FLOAT_WINDOW, enbale);
            mEditor.apply();
        }
    }

    public boolean getWehcatEnable() {
        return preferences.getBoolean(KEY_ENABLE_WECHAT, false);
    }

    public void setWechatEnable(boolean enable) {
        synchronized (preferences) {
            mEditor.putBoolean(KEY_ENABLE_WECHAT, enable);
            mEditor.apply();
        }
    }

    public boolean getQQEnable() {
        return preferences.getBoolean(KEY_ENABLE_QQ, false);
    }

    public void setQQEnable(boolean state) {
        synchronized (preferences) {
            mEditor.putBoolean(KEY_ENABLE_QQ, state);
            mEditor.apply();
        }
    }

    public int getWechatAfterOpenHongBaoEvent() {
        int defaultValue = 0;
        String result = preferences
                .getString(EVNET_WECHAT_AFTER_OPEN_PACKET, String.valueOf(defaultValue));
        try {
            return Integer.parseInt(result);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public int getWechatOpenDelayTime() {
        int defaultValue = 0;
        String result = preferences
                .getString(KEY_RED_PACKET_DELAY_TIME, String.valueOf(defaultValue));
        try {
            return Integer.parseInt(result);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public boolean autoClosePacketInterface() {
        return autoClosePacket;
    }

    public void playNotice() {
        MediaPlayer mediaPlayer = getPlayer();
        if (!mediaPlayer.isPlaying())
            mediaPlayer.start();
    }

    private MediaPlayer getPlayer() {
        if (player == null || needUpdate) {
            needUpdate = false;
            player = MediaPlayer.create(mContext, nitofyMusicID[musicIndex]);
        }
        return player;
    }

}
